package com.xbongbong.admin.service.impl;

import com.xbongbong.admin.domain.entity.RaidersProductEntity;
import com.xbongbong.admin.domain.entity.RaidersScoreEntity;
import com.xbongbong.admin.domain.entity.RaidersScoreExchangeEntity;
import com.xbongbong.admin.model.RaidersProductModel;
import com.xbongbong.admin.model.RaidersScoreExchangeModel;
import com.xbongbong.admin.model.RaidersScoreModel;
import com.xbongbong.admin.pojo.RaidersScoreExchangeInfoPojo;
import com.xbongbong.admin.pojo.RaidersScoreInfoPojo;
import com.xbongbong.admin.pojo.dto.RaidersScoreExchangeListDTO;
import com.xbongbong.admin.pojo.dto.RaidersScoreListDTO;
import com.xbongbong.admin.pojo.vo.RaidersScoreExchangeListVO;
import com.xbongbong.admin.pojo.vo.RaidersScoreListVO;
import com.xbongbong.admin.service.RaidersScoreService;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.constant.TimeConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.PageHelper;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.PageHelperUtil;
import com.xbongbong.pro.enums.raiders.enums.RaidersProductValidEnum;
import com.xbongbong.sys.model.UserModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 攻略分值相关
 *
 * @author 徐俊杰
 * @version v1.0
 * @date 2019/10/11 8:52
 * @since v1.0
 */
@Service("raidersScoreService")
public class RaidersScoreServiceImpl implements RaidersScoreService {

    @Resource
    private RaidersScoreModel raidersScoreModel;
    @Resource
    private UserModel userModel;
    @Resource
    private RaidersScoreExchangeModel raidersScoreExchangeModel;
    @Resource
    private RaidersProductModel raidersProductModel;

    @Override
    public RaidersScoreListVO list(RaidersScoreListDTO raidersScoreListDTO) throws XbbException {
        //当传入开始时间和结束时间相同时，将结束时间加一天
        Long startTime = raidersScoreListDTO.getStartTime();
        Long endTime = raidersScoreListDTO.getEndTime();
        endTime = endTimePlusOneDay(startTime, endTime);
        //查询得分记录
        String corpid = raidersScoreListDTO.getCorpid();
        Map<String, Object> scoreMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        scoreMap.put("corpid", corpid);
        scoreMap.put("del", DelEnum.NORMAL.getDel());
        scoreMap.put("startAddTime", startTime);
        scoreMap.put("endAddTime", endTime);
        scoreMap.put("page", raidersScoreListDTO.getPage());
        scoreMap.put("orderByStr", "add_time desc");
        scoreMap.put("columns", "id, creator_id, add_time, score, memo");
        PageHelper pageHelper = PageHelperUtil.getPageHelper(scoreMap, raidersScoreModel, raidersScoreListDTO.getPageSize());
        List<RaidersScoreEntity> raidersScoreList = (List<RaidersScoreEntity>) PageHelperUtil.getEntityList(scoreMap, pageHelper, raidersScoreModel);
        //获得创建人
        List<String> creatorIdList = new ArrayList<>();
        for (RaidersScoreEntity raidersScore : raidersScoreList) {
            creatorIdList.add(raidersScore.getCreatorId());
        }
        Map<String, String> userIdNameMap = userModel.getUserNameMap(corpid, creatorIdList);
        //处理需要返回的数据
        List<RaidersScoreInfoPojo> raidersScoreInfoList = new ArrayList<>();
        for (RaidersScoreEntity raidersScore : raidersScoreList) {
            RaidersScoreInfoPojo raidersScoreInfo = new RaidersScoreInfoPojo();
            BeanUtil.copyProperties(raidersScore, raidersScoreInfo);
            //处理创建人名称
            String userName = userIdNameMap.getOrDefault(raidersScore.getCreatorId(), "");
            raidersScoreInfo.setName(userName);
            //处理添加时间
            String addTimeStr = DateTimeUtil.getStringEpochSecond(raidersScore.getAddTime());
            raidersScoreInfo.setAddTime(addTimeStr);
            raidersScoreInfoList.add(raidersScoreInfo);
        }
        return new RaidersScoreListVO(pageHelper, raidersScoreInfoList);
    }

    @Override
    public RaidersScoreExchangeListVO exchangeList(RaidersScoreExchangeListDTO raidersScoreExchangeListDTO) throws XbbException {
        //当传入开始时间和结束时间相同时，将结束时间加一天
        Long startTime = raidersScoreExchangeListDTO.getStartTime();
        Long endTime = raidersScoreExchangeListDTO.getEndTime();
        endTime = endTimePlusOneDay(startTime, endTime);
        //查询兑换流水
        String corpid = raidersScoreExchangeListDTO.getCorpid();
        Map<String, Object> scoreExchangeMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        scoreExchangeMap.put("corpid", corpid);
        scoreExchangeMap.put("del", DelEnum.NORMAL.getDel());
        scoreExchangeMap.put("startAddTime", startTime);
        scoreExchangeMap.put("endAddTime", endTime);
        scoreExchangeMap.put("page", raidersScoreExchangeListDTO.getPage());
        scoreExchangeMap.put("orderByStr", "add_time desc");
        scoreExchangeMap.put("columns", "id, product_id, add_time, cost_score, surplus_score, redeem_code");
        PageHelper pageHelper = PageHelperUtil.getPageHelper(scoreExchangeMap, raidersScoreExchangeModel, raidersScoreExchangeListDTO.getPageSize());
        List<RaidersScoreExchangeEntity> raidersScoreExchangeList = (List<RaidersScoreExchangeEntity>) PageHelperUtil.getEntityList(scoreExchangeMap, pageHelper, raidersScoreExchangeModel);
        //获得产品名称
        List<Long> productIdList = new ArrayList<>();
        for (RaidersScoreExchangeEntity raidersScoreExchange : raidersScoreExchangeList) {
            productIdList.add(raidersScoreExchange.getProductId());
        }
        Map<Long, RaidersProductEntity> productIdEntityMap = raidersProductModel.getProductIdEntityMap(productIdList);
        //处理需要返回的数据
        List<RaidersScoreExchangeInfoPojo> raidersScoreExchangeInfoList = new ArrayList<>();
        for (RaidersScoreExchangeEntity raidersScoreExchange : raidersScoreExchangeList) {
            RaidersScoreExchangeInfoPojo raidersScoreExchangeInfo = new RaidersScoreExchangeInfoPojo();
            BeanUtil.copyProperties(raidersScoreExchange, raidersScoreExchangeInfo);
            //处理商品相关
            RaidersProductEntity product = productIdEntityMap.get(raidersScoreExchange.getProductId());
            if (product == null) {
                continue;
            }
            //处理过期时间
            Integer valid = product.getValid();
            RaidersProductValidEnum validEnum = RaidersProductValidEnum.getByCode(valid);
            Long addTime = raidersScoreExchange.getAddTime();
            Long validTime = getValidTime(addTime, validEnum);
            String validTimeStr = DateTimeUtil.getStringEpochSecond(validTime);
            raidersScoreExchangeInfo.setValidName(validTimeStr);
            //处理商品名称
            String productName = product.getName();
            raidersScoreExchangeInfo.setProduct(productName);
            //处理添加时间
            String addTimeStr = DateTimeUtil.getStringEpochSecond(raidersScoreExchange.getAddTime());
            raidersScoreExchangeInfo.setAddTime(addTimeStr);
            raidersScoreExchangeInfoList.add(raidersScoreExchangeInfo);
        }
        return new RaidersScoreExchangeListVO(pageHelper, raidersScoreExchangeInfoList);
    }

    /**
     * 当传入开始时间和结束时间相同时，将结束时间加一天
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return java.lang.Long
     * @author 徐俊杰
     * @date 2019/10/11 16:59
     * @since v1.0
     */
    private Long endTimePlusOneDay(Long startTime, Long endTime) {
        if (startTime != null && endTime != null && Objects.equals(startTime, endTime)) {
            endTime += TimeConstant.SECONDS_PER_DAY;
        }
        return endTime;
    }

    /**
     * 获取有效期
     *
     * @param addTime 兑换时间
     * @param validEnum 有效期枚举
     * @return java.lang.Long
     * @author 徐俊杰
     * @date 2019/10/31 17:18
     * @since v1.0
     */
    private Long getValidTime(Long addTime, RaidersProductValidEnum validEnum) {
        long validTime = 0L;
        switch (validEnum){
            case FROM_EXCHANGE_DAY_7:
                validTime = addTime + TimeConstant.SECONDS_PER_WEEK;
                break;
            case FROM_EXCHANGE_MONTH_1:
                validTime = addTime + TimeConstant.SECONDS_PER_MONTH;
                break;
            case FROM_EXCHANGE_MONTH_3:
                validTime = addTime + TimeConstant.SECONDS_THREE_MONTH;
                break;
            case FROM_EXCHANGE_MONTH_6:
                validTime = addTime + TimeConstant.SECONDS_SIX_MONTH;
                break;
            case FROM_EXCHANGE_YEAR_1:
                validTime = addTime + TimeConstant.SECONDS_PER_YEAR;
                break;
            default:
                break;
        }
        return validTime;
    }
}
